"""
运动控制器的编码器在周期测量和频率测量下的精度
"""

from pylab import *

def period_method_maxmin(Ts,Np):
    '''#计算最小周期对应的最大测量频率'''
    Tp=Ts;
    fmax=1/(Tp*Np);
    #计算最大周期对应的最小测量频率
    Tp=(2**16-1)*Ts;
    fmin=1/(Tp*Np);
    print("period mode:\tfmin=%f\tfmax=%f"%(fmin,fmax))

def freq_method_maxmin(Tsp,Np):
    Nc=1;
    fmin=Nc/(Tsp*Np)
    Nc=2**16-1
    fmax=Nc/(Tsp*Np)
    print("freq mode:\tfmin=%f\tfmax=%f"%(fmin,fmax))

def period_method(Ts,Np,freq):
    Tp=1/(freq*Np);
    #err1[f-1]=1-1/(1+f*Np*Ts)
    err=1/(1-freq*Np*Ts)-1
    return err

def freq_method(Tsf,Np,freq):
    Nc=freq*Tsf*Np
    err=1/(freq*Np*Tsf)
    return err

def cross_point(Np,Tsf,Tsp):
    b2_4ac=Tsp**2+4*Tsf*Tsp;
    f1=(-Tsp+sqrt(b2_4ac))/(2*Np*Tsf*Tsp)
    #f2=(-Tsp-sqrt(b2_4ac))/(2*Np*Tsf*Tsp)
    err=period_method(Tsp,Np,f1);
    print("cross freq=%f\terr=%f"%(f1,err))

#################################################################
Tsp=400e-9; Tsf=400e-6;
Np=4096;
err1=zeros(100);
err2=zeros(100);
freq=arange(100)+1;
cross_point(Np,Tsf,Tsp);    #计算交点
for i in arange(100):
    err1[i]=period_method(Tsp,Np,freq[i])
    err2[i]=freq_method(Tsf,Np,freq[i])
plot(freq,err1,'r',freq,err2)   #显示误差曲线
show()